#!/bin/bash -x

# ./test-uossign.sh 
# -k 原始补丁仓库 
# -l 原始补丁列表 
# -c cve 
# -i 任务id 
# -p 已经导出的补丁列表
# -f 来源 (euler/anolis/mainline)
# -r 使用“git rebase -i”为指定范围的补丁打上签名
# ./test-uossign.sh -k ../kernel -l /tmp/list -c CVE-2023-35788 -i T123456 -f anolis -p /tmp/xfs

while [ -n "$1" ]
do
	case "$1" in
	-k)
		origin_repo="$2"
	;;
	-l)
		origin_list="$2"
	;;
	-c)
		cve="$2"
	;;
	-i)
		id="$2"
	;;
	-f)
		from="$2"
	;;
	-p)
		pdir="$2"
	;;
	esac
	shift 2
done

#if [ -z "$origin_repo" ] || [ -z "$origin_list" ] || [ -z "$from" ] || [ -z "$pdir" ]; then
#	echo 缺少参数！！！
#	exit
#fi

lspdir=`ls $pdir`
cd $pdir

i=0
#from_commitid=($(awk '{print$1}' $origin_list))
tmpfile=.patch.$$
newdir="new"
mkdir $newdir

for patch in $lspdir
do
	line_start=4
	line_end=4
	#commit_id=`GIT_DIR=$origin_repo/.git git log -1 ${from_commitid[$i]} --pretty=format:"%H"`

	line=`sed -n '5p' $patch`
	if [ "$line" != "" ]; then
		let line_end+=1
	fi
	
#	cat $patch | sed $line_start' s/] /] '$from': /' | \
#		sed $(bc <<< "$line_end + 1")' i\\ncommit '$commit_id' '$from > $tmpfile
	cat $patch | sed $line_start' s/] /] '$from': /' > $tmpfile
	if [ -n "$id" ]; then
		sed -i $line_end' s/$/ ['$id']/' $tmpfile
	fi
#	cat $patch | sed $line_start' s/] /] '$from': /' | \
#		sed $line_end' s/$/ ['$id']/' | \
#		sed $(bc <<< "$line_end + 1")' i\\ncommit '$commit_id' '$from > new/$patch
	mv $tmpfile $newdir/$patch
	let i+=1
done

